package cn.bytecollege.service;

import cn.bytecollege.mapper.UserAddressMapper;
import cn.bytecollege.mapper.UserMapper;
import cn.bytecollege.model.Goods;
import cn.bytecollege.model.User;
import cn.bytecollege.model.UserAddress;
import cn.bytecollege.vo.CommunityWorkerVO;
import cn.bytecollege.vo.DistributionWorkerVO;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Log
public class SystemService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 查询社区人员信息
     * @return
     */
    public List<CommunityWorkerVO> getCommunityWorker(){
//        查询权限为社区人员的user
        QueryWrapper<User>  userQueryWrapper = new QueryWrapper<User>()
                .eq("power", "社区人员").select("user_id","username");
//    将社区人员存放至userList中
        List<User> userList = userMapper.selectList(userQueryWrapper);
        List<CommunityWorkerVO> communityWorkerVOList = new ArrayList<>();
//    遍历userList ,获得社区人员的id，通过id获得社区人员的地址和电话号码
        for (User u:userList) {
//        获得社区人员的id
            Integer userId = u.getUserId();
//        通过用户id查找用户地址、电话号码
            QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                    .eq("user_id", userId).select("address","telephone");
            UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
            CommunityWorkerVO communityWorkerVO = new CommunityWorkerVO();
            communityWorkerVO.setUserId(userId);
            communityWorkerVO.setUsername(u.getUsername());
            communityWorkerVO.setAddress(userAddress.getAddress());
            communityWorkerVO.setTelephone(userAddress.getTelephone());
//            log.info(communityWorkerVO.toString());
            communityWorkerVOList.add(communityWorkerVO);
        }
//        log.info(communityWorkerVOList.toString());
        return communityWorkerVOList;
    }
    /**
     * 通过id查询社区人员
     */
    public CommunityWorkerVO getCommunityWorker(Integer id){
//        查询权限为社区人员的user
        QueryWrapper<User>  userQueryWrapper = new QueryWrapper<User>()
                .eq("power", "社区人员").select("user_id","username");
//    将社区人员存放至userList中
        List<User> userList = userMapper.selectList(userQueryWrapper);
//    遍历userList ,获得社区人员的id，通过id获得社区人员的地址和电话号码
        for (User u:userList) {
//        获得社区人员的id
            Integer userId = u.getUserId();
//        通过用户id查找用户地址、电话号码
            QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                    .eq("user_id", userId).select("address","telephone");
//        将查找信息封装到userAddressList中,并将userAddressList封装到userList
            UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
            if (userId==id){
                CommunityWorkerVO communityWorkerVO = new CommunityWorkerVO();
                communityWorkerVO.setUserId(userId);
                communityWorkerVO.setUsername(u.getUsername());
                communityWorkerVO.setAddress(userAddress.getAddress());
                communityWorkerVO.setTelephone(userAddress.getTelephone());
                return communityWorkerVO;
            }
        }
        return null ;
    }
    /**
     * 删除社区人员
     */@Transactional
    public int delCommunityWorker(Integer id){
        //获取在userAddress中的信息
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>().eq("user_id", id);
        UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
        //通过ID删除信息
        userAddressMapper.deleteById(userAddress.getUserAddressId());
        //测试回滚
        //        int i=5/0;
        //通过ID删除信息
        userMapper.deleteById(id);
        return 1;
    }
    /**
     * 修改社区人员信息
     */
    public int modCommunityWorker(CommunityWorkerVO communityWorkerVO){
        //获取对应的user的 QueryWrapper
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>().eq("user_id", communityWorkerVO.getUserId());
        //将user的信息存放到
//        log.info(userQueryWrapper.toString());
        //创建user保存数据
        User user = userMapper.selectById(communityWorkerVO.getUserId());
//        log.info(user.toString());
        //创建对应的userAddressQueryWrapper
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                .eq("user_id", user.getUserId());
        //创建userAddress保存数据
        UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
//        log.info(userAddress.toString());
        //修改userAddress中的数据
        userAddress.setAddress(communityWorkerVO.getAddress());
        userAddress.setTelephone(communityWorkerVO.getTelephone());
        //修改user中的数据
        user.setUsername(communityWorkerVO.getUsername());
        //更新到数据库
        userMapper.updateById(user);
        userAddressMapper.updateById(userAddress);
        return 1;
    }
    /**
     * 添加社区人员
     */
    public int addCommunityWorker(CommunityWorkerVO communityWorkerVO){
        //创建user保存数据
        User user = new User();
        //修改user中的数据
        user.setUsername(communityWorkerVO.getUsername());
        String password = String.valueOf(DigestUtil.md5(user.getPassword()));
        user.setPassword(password);        user.setPower("社区人员");
        //更新到数据库
        userMapper.insert(user);
        //创建userAddress保存数据
        UserAddress userAddress = new UserAddress();
        //修改userAddress中的数据
        userAddress.setUserId(user.getUserId());
        userAddress.setAddress(communityWorkerVO.getAddress());
        userAddress.setTelephone(communityWorkerVO.getTelephone());
        userAddress.setRealName(communityWorkerVO.getRealName());
        //更新到数据库
        userAddressMapper.insert(userAddress);
        return 1;
    }

    /**
     * 根据姓名查找
     */
    public List<CommunityWorkerVO> findByCommunityWorkerName(String username){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>().eq("username", username);
        User user = userMapper.selectOne(userQueryWrapper);
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                .eq("user_Id",user.getUserId());
        UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
        CommunityWorkerVO communityWorkerVO = new CommunityWorkerVO();
        communityWorkerVO.setUserId(user.getUserId());
        communityWorkerVO.setUsername(user.getUsername());
        communityWorkerVO.setTelephone(userAddress.getTelephone());
        communityWorkerVO.setAddress(userAddress.getAddress());
        List<CommunityWorkerVO> list = new ArrayList<>();
        list.add(communityWorkerVO);
//        log.info(list.toString());
        return list;
    }

    /**
     * 查询配送人员
     */
    public List<DistributionWorkerVO> getDistributionWorker(){
//        查询权限为社区人员的user
        QueryWrapper<User>  userQueryWrapper = new QueryWrapper<User>()
                .eq("power", "配送人员").select("user_id","username");
//    将社区人员存放至userList中
        List<User> userList = userMapper.selectList(userQueryWrapper);
        List<DistributionWorkerVO> distributionWorkerVOList = new ArrayList<>();
//    遍历userList ,获得社区人员的id，通过id获得社区人员的地址和电话号码
        for (User u:userList) {
//        获得社区人员的id
            Integer userId = u.getUserId();
//        通过用户id查找用户地址、电话号码
            QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                    .eq("user_id", userId).select("address","telephone");
            UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
            DistributionWorkerVO distributionWorkerVO= new DistributionWorkerVO();
            distributionWorkerVO.setUserId(userId);
            distributionWorkerVO.setUsername(u.getUsername());
            distributionWorkerVO.setAddress(userAddress.getAddress());
            distributionWorkerVO.setTelephone(userAddress.getTelephone());
            distributionWorkerVOList.add(distributionWorkerVO);
        }
//        log.info(distributionWorkerVOList.toString());
        return distributionWorkerVOList;
    }
    /**
     * 通过id查询社区人员
     */
    public DistributionWorkerVO getDistributionWorker(Integer id){
//        查询权限为社区人员的user
        QueryWrapper<User>  userQueryWrapper = new QueryWrapper<User>()
                .eq("power", "配送人员").select("user_id","username");
//    将社区人员存放至userList中
        List<User> userList = userMapper.selectList(userQueryWrapper);
//    遍历userList ,获得社区人员的id，通过id获得社区人员的地址和电话号码
        for (User u:userList) {
//        获得社区人员的id
            Integer userId = u.getUserId();
//        通过用户id查找用户地址、电话号码
            QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                    .eq("user_id", userId).select("address","telephone");
//        将查找信息封装到userAddressList中,并将userAddressList封装到userList
            UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
            if (userId==id){
                DistributionWorkerVO distributionWorkerVO = new DistributionWorkerVO();
                distributionWorkerVO.setUserId(userId);
                distributionWorkerVO.setUsername(u.getUsername());
                distributionWorkerVO.setAddress(userAddress.getAddress());
                distributionWorkerVO.setTelephone(userAddress.getTelephone());
                return distributionWorkerVO;
            }
        }
        return null ;
    }
    /**
     * 删除配送人员
     */
    public int delDistributionWorker(Integer id){
        //获取在userAddress中的信息
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                .eq("user_id", id);
        UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
        //通过ID删除信息
        userAddressMapper.deleteById(userAddress.getUserAddressId());
        //测试回滚
        //        int i=5/0;
        //通过ID删除信息
        userMapper.deleteById(id);
        return 1;
    }
    /**
     * 修改配送人员信息
     */
    public int modDistributionWorker(DistributionWorkerVO DistributionWorkerVO){
        //获取用户id
        Integer userId = DistributionWorkerVO.getUserId();
        //将user的信息存放到user
        User user = userMapper.selectById(userId);
        //修改用户名
        user.setUsername(DistributionWorkerVO.getUsername());
        //创建userAddressQueryWrapper找到对应的userAddress
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                .eq("user_id", userId);
        UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
        //修改userAddress中的信息
        userAddress.setAddress(DistributionWorkerVO.getAddress());
        userAddress.setTelephone(DistributionWorkerVO.getTelephone());
        //更新数据
        userMapper.updateById(user);
        userAddressMapper.updateById(userAddress);
        return 1;
    }
    /**
     * 添加配送人员
     */
    public int addDistributionWorker(DistributionWorkerVO distributionWorkerVO){
        //创建user保存数据
        User user = new User();
        //修改user中的数据
        user.setUsername(distributionWorkerVO.getUsername());
        String password = String.valueOf(DigestUtil.md5(user.getPassword()));
        user.setPassword(password);
        user.setPower("配送人员");
        //更新到数据库
        userMapper.insert(user);
        //创建userAddress保存数据
        UserAddress userAddress = new UserAddress();
        //修改userAddress中的数据
        userAddress.setUserId(user.getUserId());
        userAddress.setAddress(distributionWorkerVO.getAddress());
        userAddress.setTelephone(distributionWorkerVO.getTelephone());
        userAddress.setRealName(distributionWorkerVO.getRealName());
        //更新到数据库
        userAddressMapper.insert(userAddress);
        return 1;
    }
    /**
     * 根据姓名查找
     */
    public List<DistributionWorkerVO> findByDistributionWorkerName(String username){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>().eq("username", username);
        User user = userMapper.selectOne(userQueryWrapper);
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<UserAddress>()
                .eq("user_Id",user.getUserId());
        UserAddress userAddress = userAddressMapper.selectOne(userAddressQueryWrapper);
        DistributionWorkerVO distributionWorkerVO = new DistributionWorkerVO();
        distributionWorkerVO.setUserId(user.getUserId());
        distributionWorkerVO.setUsername(user.getUsername());
        distributionWorkerVO.setTelephone(userAddress.getTelephone());
        distributionWorkerVO.setAddress(userAddress.getAddress());
        List<DistributionWorkerVO> list = new ArrayList<>();
        list.add(distributionWorkerVO);
        
//        log.info(list.toString());
        return list;
    }

}
